package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.entity.User;
import com.qf.result.ResultMsg;
import com.qf.service.IUserService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘亚康
 * @Date 2020/3/26
 * @Version 1.0'
 * @Description
 */
@Controller
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Reference
    private IUserService userService;

    /**
     * 分页查询所有用户
     *
     * @param page
     * @param map
     * @return
     */
    @RequestMapping("/getAllUser")
    public String getAllUser(Page<User> page, ModelMap map) {
        Page<User> dubboPage = userService.getUserByPage(page);
        map.put("url", "user/getAllUser");
        map.put("page", dubboPage);
        return "user/userList";
    }

    /**
     * 添加用户
     *
     * @param user
     * @return success or fail
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public ResultMsg addUser(User user) {
        log.info("要添加的用户为：" + user);
        int result = userService.add(user);
        if (result > 0) {
            return ResultMsg.success();
        } else {
            return ResultMsg.fail();
        }
    }

    /**
     * 获取要修改的用户
     *
     * @param id
     * @param map
     * @return 用户修改页面
     */
    @RequestMapping("getEditUser/{id}")
    public String getEditUser(@PathVariable("id") Integer id, ModelMap map) {

        User u = userService.getById(id);
        log.info("要修改的用户为：" + u);
        map.put("user", u);
        return "user/editUser";
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return 修改的结果
     */
    @RequestMapping("editUser")
    @ResponseBody
    public ResultMsg editUser(User user) {
        int result = userService.update(user);
        if (result > 0) {
            return ResultMsg.success();
        } else {
            return ResultMsg.fail();
        }
    }

    /**
     * 根据用户id删除该用户
     *
     * @param id
     * @return 删除结果
     */
    @RequestMapping("delUser/{id}")
    @ResponseBody
    public ResultMsg delUser(@PathVariable Integer id) {
        log.info("要删除的用户id:" + id);
        int result = userService.delete(id);
        if (result > 0) {
            return ResultMsg.success();
        } else {
            return ResultMsg.fail();
        }
    }

    /**
     * 批量删除数据
     *
     * @param userIdList
     * @return 删除结果
     */
    @RequestMapping("batchDelUser")
    @ResponseBody
    public ResultMsg batchDelUser(@RequestParam("userId[]") List<Integer> userIdList) {
        int resultNum = 0;
        log.info("批量删除数据：" + userIdList.toString() + "需要删除数据 " + userIdList.size() + " 条");
        for (Integer userId : userIdList) {
            int result = userService.delete(userId);
            resultNum = resultNum + result;
        }
        log.info("共删除数据 " + resultNum + " 条");
        if (resultNum == userIdList.size()) {
            return ResultMsg.success();
        } else {
            return ResultMsg.fail();
        }
    }
}
